$('input').iCheck({
	checkboxClass: 'icheckbox_square-red',
	radioClass: 'iradio_square-red'
});
$('.block-task input').on('ifToggled', function(){
 $(this).closest('li').find('.task-name').toggleClass('text-del')
});
$('.tab-record input').on('ifToggled', function(){
 $(this).closest('li').find('.record-task').find('span').toggleClass('text-del')
});
$('.im-dialog-task input').on('ifToggled', function(){
 $(this).closest('.im-dialog-body').find('.dialog-task-name').toggleClass('text-del')
});
//左侧导航
$('.sidebar-im').on('click','.im-item',function(){
	$(this).closest('.tab-pane').find('li').removeClass('active');
	$(this).parent('li').addClass('active');
});

//select
$('.selectpicker').selectpicker();

//侧滑
$('.im-mainarea').on('click','.trigger-im-task', function() {
	$('.na-menu').toggleClass('na-menu-open');
	$('.handler').toggleClass('hide');
});

$('.im-mainarea').on('click','.trigger-record', function() {
	$('.asideRight').toggleClass('asideRight-open');
});

//弹窗-邀请成员
$('body').on('click','.btn-yaoqing',function(){
	$('.popup-yaoqing').show();
})
//弹窗-退出讨论组
$('body').on('click','.btn-group-quit',function(){
	$('.popup-group-quit').show();
})

//弹窗-新建讨论组-公开
$('.im-sidebar-tab').on('click','.btn-addgroup',function(){
	$('.popup-addgroup').show();
	return false;
})
//弹窗-新建讨论组-私聊
$('.im-sidebar-tab').on('click','.btn-addgroup-private',function(){
	$('.popup-addgroup-private').show();
	return false;
});

$('.select-yaoqing').select2({
 	theme: "default myselect",
	placeholder: "+用户名"
});
//typeahead
var im_user = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    prefetch: 'js/im_user.json?1'
  });

$('.search-im .typeahead').typeahead(null, {
    name: 'searchim',
	display: 'value',
    source: im_user
});


$('#range-search').multiselect({
	 //maxHeight: 220,
	 buttonClass: 'btn btn-range',
	 nonSelectedText: '全部范围',
	 enableFiltering: false,	 
	 dropRight: true
});

//textarea 变化时 对话框随着变化
$('.reply-input').on('keyup','.autoheight',function(){
	dialogHeight();
});


//设高度 for myscroll
function sidebarTabH(){
	var eleH1,eleH2,eleH3,eleH4,eleH5,targetH;
	eleH1 = $('.sidebar').height();
	eleH2 = $('.search-im').outerHeight();
	eleH3 = $('.im-sidebar-tab>.nav-tabs').outerHeight();
	
	targetH = eleH1 - eleH2 - eleH3-24;
	$('.im-sidebar-tab .tab-pane').css({'height':targetH+'px'});	
}
function messageRecordH(){
	var eleH1,eleH2,eleH3,eleH4,eleH5,targetH;
	eleH1 = $('.asideRight').height();
	eleH2 = $('.asideRight-op').outerHeight();
	eleH3 = $('.record-pagnation').outerHeight();
	eleH4 = $('.block-title').outerHeight();
	
	targetH = eleH1 - eleH2 - eleH3-eleH4;
	$('.record-ul').css({'height':targetH+'px'});	
}
/*对话框高度*/
function dialogHeight(){
	var mainH,titleH,dialogH,inputH;
	mainH = $('.im-main').height();
	titleH = $('.im-page-header').outerHeight();
	inputH = $('.task-sidebarR-dialog').outerHeight();
	dialogH = (mainH - titleH -inputH-20);
	$('.im-container').css({'height':dialogH+'px'});
}
//function imSidebarRightMain(){
//	var eleH1,eleH2,eleH3,eleH4,eleH5,targetH;
//	eleH1 = $('.im-sidebar-right').height();
//	eleH2 = $('.im-user-list').outerHeight();
//	eleH3 = $('.im-sidebar-tab>.nav-tabs').outerHeight();
//	
//	targetH = eleH1 - eleH2 - eleH3-24;
//	$('.im-sidebar-tab .tab-pane').css({'height':targetH+'px'});
//}


$(function(){
    sidebarTabH();
    dialogHeight();
    messageRecordH();
    $(window).on('resize',function(){
    	sidebarTabH();
	    dialogHeight();
	    messageRecordH();
    });
});




//创建事务
$('.im-dialog').on('click','.btn-task-create',function(){
	$('.popup-task').show();
});
$('.task-sidebarR-dialog').on('click','.btn-task-create',function(){
	$('.popup-task').show();
});
//datetime picker
$('.hasdate').datetimepicker({
	lang:'ch',
	format:'Y-m-d H:i',
	yearStart:2015,
	yearEnd:2050
});

//task more show/hide
$('body').on('click','.btn-show',function(){
	var bol = $(this).find('i').hasClass('gicon-todown');

	if(bol){
		$('.row-more').slideDown(300);	
		$('.btn-show').find('span').text('less');
		$('.btn-show').find('i').removeClass('gicon-todown').addClass('gicon-totop');
		return false;
	}else{
		$('.row-more').slideUp(200);	
		$('.btn-show').find('span').text('more');
		$('.btn-show').find('i').removeClass('gicon-totop').addClass('gicon-todown');
		return false;
	}
});

//fupload
$('.btn-file-add').on('click',function(){
	$('.plusarea').closest('.row-more').removeClass('hidden');
});

$('.reply-add').on('change','input[type="file"]',function(){
	$(this).closest('.reply-add').removeClass('open');
});

function fupload(address){
	var dt="已添加附件:",				
		p = '<p><a href="javascript:;">'+address+'<i class="ml20 glyphicon glyphicon-remove"></i></a></p>',
	    hasdt = $('.plusarea:contains('+dt+')').length;			
	$('.plusarea').show();
	if(hasdt){
		$('.plusarea').find('dd').eq(0).append(p);
	}else{
		$('.plusarea').find('dt').eq(0).text(dt).css({'border':'1px solid #f00'});
		$('.plusarea').find('dd').eq(0).append(p);
	}
}

$('.btn-file-add').on('click',function(){
	fupload('test.zip');				
	$('.row-more').slideDown(600);	
	$('.btn-show').find('span').text('less');
	$('.btn-show').find('i').removeClass('gicon-todown').addClass('gicon-totop');
});		

$('.plusarea dd').on('click','.glyphicon-remove',function(){
	var length = $(this).closest('dd').find('p').size();
	if(length==1){
		$(this).closest('dd').prev('dt').text('');
		$(this).closest('p').remove();	
		$('.plusarea').hide();
	}else{				
	$(this).closest('p').remove();			
	}			
});